home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / ppl4c10.zip / TERM_IO.C < prev    next >
Text File  |  1995-02-11  |  4KB  |  188 lines

  1. /*** TERM_IO.C ***/
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "pcl4c.h"
  7. #include "term_io.h"
  8. #include "ascii.h"
  9. #include "dos_io.h"
  10. #include "win_io.h"
  11.  
  12. #define FALSE 0
  13. #define TRUE !FALSE
  14.  
  15. static int EchoFlag = FALSE;
  16. static char EnterMsg[] = "Enter Filename:";
  17.  
  18. #define QUE_MASK 0x0f
  19.  
  20. static int QueueEnable = FALSE;
  21.  
  22. static  char Temp[80];
  23.  
  24. static struct
  25. {int  Head;
  26.  int  Tail;
  27.  char Char[QUE_MASK+1];
  28.  char Cmd[QUE_MASK+1];
  29. } Queue;
  30.  
  31. void QueueOn(void)
  32. {WinPutString(SCR_WIN,"Queue On\n");
  33.  Queue.Head = 0;
  34.  Queue.Tail = 0;
  35.  QueueEnable = TRUE;
  36. }
  37.  
  38. void ShowQueue(void)
  39. {int h, t;
  40.  char Cmd;
  41.  WinPutString(SCR_WIN,"ShowQueue\n");
  42.  h = Queue.Head;
  43.  t = Queue.Tail;
  44.  while(1)
  45.    {if(h==t) break;
  46.     Cmd = Queue.Cmd[h];
  47.     if(Cmd=='T') sprintf(Temp,"   T  %2x\n",Queue.Char[h]);
  48.     else sprintf(Temp,"%c     %2x\n",Cmd,Queue.Char[h]);
  49.     h = (h + 1) & QUE_MASK;
  50.     WinPutString(SCR_WIN,Temp);
  51.    }
  52. }
  53.  
  54. static void EnQueue(char Cmd,char C)
  55. {int t;
  56.  t = (Queue.Tail + 1) & QUE_MASK;
  57.  if(t==Queue.Head)
  58.    {/* must de-queue to make room */
  59.     Queue.Head = (Queue.Head + 1) & QUE_MASK;
  60.    }
  61.  Queue.Cmd[Queue.Tail]  = Cmd;
  62.  Queue.Char[Queue.Tail] = C;
  63.  Queue.Tail = t;
  64. }
  65.  
  66. void MsgEcho(int Flag)
  67. {EchoFlag = Flag;
  68. }
  69.  
  70. /* write message to right status line */
  71.  
  72. void WriteMsg(char *MsgPtr)
  73. {if(EchoFlag)
  74.    {WinPutString(SCR_WIN,MsgPtr);
  75.     WinPutChar(SCR_WIN,'\n');
  76.    }
  77.  WinClear(RGT_WIN);
  78.  WinPutChar(RGT_WIN,' ');
  79.  WinPutString(RGT_WIN,MsgPtr);
  80.  WinGetPos(SCR_WIN,NULL,NULL);
  81. }
  82.  
  83. /*** write message + integer parm */
  84.  
  85. void WriteIntMsg(char *MsgPtr, int Parm)
  86. {char Temp[50];
  87.  sprintf(Temp,"%s %d", MsgPtr, Parm);
  88.  WriteMsg(Temp);
  89. }
  90.  
  91. /*** write message + hexidecimal parm */
  92.  
  93. void WriteHexMsg(char *MsgPtr, int Parm)
  94. {char Temp[50];
  95.  sprintf(Temp,"%s 0x%x", MsgPtr, Parm);
  96.  WriteMsg(Temp);
  97. }
  98.  
  99. /*** write message + long parm */
  100.  
  101. void WriteLongMsg(char *MsgPtr, long Parm)
  102. {char Temp[50];
  103.  sprintf(Temp,"%s %ld", MsgPtr, Parm);
  104.  WriteMsg(Temp);
  105. }
  106.  
  107. /*** read message */
  108.  
  109. void ReadMsg(char *Buffer,int Length,int StartCol)
  110. {WinSetPos(RGT_WIN,0,StartCol);
  111.  WinGetString(RGT_WIN,Buffer,Length);
  112.  WinGetPos(SCR_WIN,NULL,NULL);
  113. }
  114.  
  115. /*** display the error text ***/
  116.  
  117. void SayError(int Port, char *ptr)
  118. {sprintf(Temp,"ERROR! COM%d : %s",1+Port,ptr);
  119.  WriteMsg(Temp);
  120.  /* cancel remote */
  121.  CharPut(Port,CAN);
  122.  CharPut(Port,CAN);
  123.  CharPut(Port,CAN);
  124. } /* end SayError */
  125.  
  126. /*** output character to serial port ***/
  127.  
  128. int CharPut(int Port, char ch)
  129. {int i, rc;
  130.  /* transmit character */
  131.  rc = SioPutc(Port,ch);
  132.  if(QueueEnable) EnQueue('T',ch);
  133.  if(rc<0)
  134.      {sprintf(Temp,"SioPutc Error: COM%d: ",1+Port);
  135.       WinPutString(SCR_WIN,Temp);
  136.       SioError(rc);
  137.       WinPutChar(SCR_WIN,'\n');
  138.       SioDone(Port);
  139.       exit(1);
  140.      }
  141.  return(rc);
  142. }
  143.  
  144. /*** receive character from serial port ***/
  145.  
  146. int CharGet(int Port, int Timeout)
  147. {int rc;
  148.  rc = SioGetc(Port,Timeout);
  149.  if(rc<-1)
  150.      {sprintf(Temp,"SioGetc Error: COM%d: ",1+Port);
  151.       WinPutString(SCR_WIN,Temp);
  152.       SioError(rc);
  153.       exit(1);
  154.      }
  155.  if(QueueEnable&&(rc>=0)) EnQueue('R',(char)rc);
  156.  return(rc);
  157. }
  158.  
  159. /*** fetch name from user if Filespec[] is empty ***/
  160.  
  161. int FetchName(char *FileName)
  162. {/* already have filename ? */
  163.  if(strlen(FileName) > 0) return TRUE;
  164.  /* get filename from user */
  165.  WriteMsg(EnterMsg);
  166.  ReadMsg(FileName,20,1+strlen(EnterMsg));
  167.  /* got non-NULL name ? */
  168.  if(strlen(FileName)>0) return TRUE;
  169.  else return FALSE;
  170. }
  171.  
  172. /* display CPS */
  173.  
  174. void WriteCPS(long StartTics,long FileBytes,char *Filename,int Skipped)
  175. {int  CPS;
  176.  long Tics;
  177.  long Secs;
  178.  char Temp[80];
  179.  if(*Filename=='\0') return;
  180.  if(!Skipped)
  181.    {Secs = (SioTimer() - StartTics) / 18;
  182.     if(Secs>0) CPS = (int)(FileBytes / Secs);
  183.     else Skipped = TRUE;
  184.    }
  185.  if(Skipped) sprintf(Temp,"%s skipped (%ld bytes)\n",Filename,FileBytes);
  186.  else sprintf(Temp,"%s transferred @ %d CPS (%ld bytes)\n",Filename,CPS,FileBytes);
  187.  WinPutString(SCR_WIN,Temp);
  188. }